<template>
  <el-dialog v-model="visible" :close-on-click-modal="false" :close-on-press-escape="false" v-loading="dialogLoading">
    <div style="display: flex; justify-content: space-between;margin-bottom: 20px;padding-top: 10px">
      <div style="display: flex; align-items: center">
        <div class="p-flex">
          <span style="width: 60px">搜索</span>
          <el-input v-model="tableAddr" maxlength="44" @keyup.enter="inputSearch()"></el-input>
        </div>
        <div class="p-flex">
          <!--          已选{{ checked.length }}-->
          <!--          <el-checkbox v-model="checkedlook">只看已选（{{ checked.length }}）</el-checkbox>-->
        </div>
      </div>
      <div>
        <el-button type="primary" @click="screenshot()">抓图</el-button>
      </div>
    </div>

    <el-table :data="crmListall" row-key="id" @select="selectChange" ref="multipleTable" @select-all="selectAllChange"
              @selection-change="handleSelectionChange" height="400">
      <el-table-column type="selection" width="55"/>
      <el-table-column property="name" label="杆塔名称"/>
    </el-table>
  </el-dialog>
</template>

<script>
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";

export default {
  data() {
    return {
      tableData: [], //初始数据
      crmListall: [], //筛选展示数据
      checkAll: false,
      checked: [],
      tableAddr: "",
      secondNum: 5,
      checkedlook: false,
      cirblooun: false,
      setIntervalTd: null,
      visible: false,
      dialogLoading: false
    }
  },
  watch: {
    checkedlook() {
      if (this.checkedlook) {
        this.crmListall = this.checked;
      } else {
        this.crmListall = this.tableData;
      }
    }
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.getMapList();
      });
    },
    getMapList() {
      this.tableData = [];
      baseService.get("/KxStation/kxstation/getListImgsChannelTree").then((res) => {
        this.tableData = res.data.list;
        this.crmListall = this.tableData;
      });
    },
    inputSearch() {
      this.crmListall = this.fuzzySearch(this.tableAddr, this.tableData);
    },
    fuzzySearch(keyword, data) {
      return data.filter((item) => item.addr.includes(keyword));
    },
    selectChange(selection) {
      this.checked = selection;
    },
    selectAllChange(selection) {
      this.checked = selection;
    },
    handleSelectionChange(selection) {
      //处理数据
      //。。。
      // checked.value = selection.map((item) => item.id);
    },
    screenshot() {
      if (this.checked.length <= 0) {
        return ElMessage.warning("请先选择杆塔！");
      }
      const pamers = this.checked.reduce((accumulator, current) => {
        const existingItem = accumulator.find(item => item.stationId === current.pid);
        if (existingItem) {
          existingItem.channelList.push(current.name.charAt(current.name.length - 1) * 1);
        } else {
          accumulator.push({
            stationId: current.pid,
            channelList: [current.name.charAt(current.name.length - 1) * 1]
          });
        }
        return accumulator;
      }, []);


      pamers.shift();
      this.dialogLoading = true;

      baseService.post(`/KxWarningPic/kxwarningpic/sendCmdTakePhone`, pamers).then((res) => {
        if (res.code !== 0) {
          return ElMessage.error(res.msg);
        }
        ElMessage.success("命令下发成功，请稍后刷新列表！");
        this.dialogLoading = false;
        this.visible = false;
        this.$emit("close");
      }).catch(() => {
        this.dialogLoading = false;
      });
    }
  }
}
</script>

<style scoped lang="scss">
.p-flex {
  display: flex;
  align-items: center;
  padding-right: 20px;
}
</style>
